import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

let router = new Router({
  /*  配置路由 */
  routes: [
    //默认打开页--首页
    {
      name: 'default',  //默认
      path: '/',
      redirect: { name: 'index' }
    },
    //首页
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue'),
      meta: {
        title: "微笑中医",
        tabShow:true
      }
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue'),
      meta: {
        title: "登录",
        tabShow:false
      }
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue'),
      meta: {
        title: "注册",
        tabShow:false
      }
    },
    {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue'),
      meta: {
        title: "个人中心",
        tabShow:true
      }
    },
    //编辑个人信息
    {
      name: 'editProfile',
      path: '/editProfile/:id',
      component: () => import('@/views/editProfile.vue'),
      meta: {
        title: "编辑个人信息",
        tabShow:false
      }
    },
    {
      name: 'article',
      path: '/article/:id',
      component: () => import('@/views/articleDetail.vue'),
      meta: {
        title: "文章详情",
        tabShow:false
      }
    },
    {
      name: 'book',
      path: '/book/:id',
      component: () => import('@/views/bookDetail.vue'),
      meta: {
        title: "书籍详情",
        tabShow:false
      }
    },
    {
      name: 'study',
      path: '/study',
      component: () => import('@/views/study.vue'),
      meta: {
        title: "探索",
        tabShow:true
      }
    },   
    {
      name: 'relax',
      path: '/relax',
      component: () => import('@/views/studyDetail.vue'),
      meta: {
        title: "轻松一刻",
        tabShow:false
      }
    },
    {
      name: 'search',
      path: '/search',
      component: () => import('@/views/search.vue'),
      meta: {
        title: "搜索",
         tabShow:true
      }
    },
    {
      name: 'collect',
      path: '/collect',
      component: () => import('@/views/collect.vue'),
      meta: {
        title: "搜索",
         tabShow:true
      }
    }
  ]
})

// 添加全局的导航守卫--前置守卫,在跳转前触发
//他的作用是限制用户的路由跳转, 路由能不能跳转,先经过守卫进行判断并处理
router.beforeEach((to, from, next) => {
  let arr = ['/persnal', '/myeditprofile']
  // for (let i = 0; i < arr.length; i++) {
  //我们需要判断一下用户当前目标路由是否是需要验证的路由
  if (arr.indexOf(to.path) !== -1) {
    //判断用户是否有合法的登录状态,如果有则继续跳转,如果没有则重定向到登录
    let token = localStorage.getItem('heima-headline')
    if (token) {
      next()
    } else {
      //next的参数与之前的$router.push方法的参数一致
      next({
        name: 'login'
      })
    }
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
})
export default router